package com.link.base.wechat.basic.util;

import com.link.core.cllog.LCLogger;
import java.security.MessageDigest;

/**
 * 计算公众平台的消息签名接口，类似与aes/SHA1
 *
 * @author ouyangzhenqiang
 * @date 2019/02/28 18:23
 */
public class Sha1Util {

    /**
     * 用SHA1算法生成安全签名_JS接口
     *
     * @param ticket 票据
     * @return 安全签名
     * @throws
     */
    public static String getJsSHA1(String ticket) {
        try {
            String str = ticket;
            // SHA1签名生成
            MessageDigest md = MessageDigest.getInstance("SHA-1");
            md.update(str.getBytes());
            byte[] digest = md.digest();

            StringBuffer hexStr = new StringBuffer();
            String shaHex = "";
            for (int i = 0; i < digest.length; i++) {
                shaHex = Integer.toHexString(digest[i] & 0xFF);
                if (shaHex.length() < 2) {
                    hexStr.append(0);
                }
                hexStr.append(shaHex);
            }
            return hexStr.toString();
        } catch (Exception e) {
            LCLogger.withException(e);
            return "error";
        }
    }
}
